package edu.nf.llmtest.web;

import edu.nf.llmtest.common.VO.ResultVO;
import edu.nf.llmtest.common.base.BaseController;
import edu.nf.llmtest.model.entity.User;
import edu.nf.llmtest.service.TokenService;
import edu.nf.llmtest.service.UserService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.UUID;

/**
 * @ Author bin
 * @ Create  2025/10/20 下午8:08
 **/
@RestController
@RequestMapping("/user")
@Slf4j
@RequiredArgsConstructor
public class UserController extends BaseController {
    private final UserService userService;
    private final TokenService tokenService;

    /**
     * 登录
     * @param email 邮箱
     * @param password 密码
     * return 登录成功返回token和用户信息
     * */
    @PostMapping("/login")
    public ResultVO<HashMap<String, Object>> login(@Param("email") String email,
                                                   @Param("password") String password) {
        HashMap<String, Object> requestBody = new HashMap<>();
        User user = userService.login(email, password);
        //生成token
        String token = tokenService.createTokenForUser(user.getEmail());
        requestBody.put("token", token);
        requestBody.put("user", user);
        return success(requestBody);
    }

    @PostMapping("/register")
    public ResultVO<String> register(String email, String password) {
        if (userService.UserIsExist(email) != null) {
            return error(400, "用户已存在");
        }
        userService.register(email, password);
        return success();
    }

    @PostMapping("/deleteUser")
    public ResultVO<String> delete(String email) {
        if (userService.UserIsExist(email) == null) {
            return error(10001, "用户不存在");
        }
        userService.deleteUser(email);
        return success();
    }
}
